package com.qyer.search.threading;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Executors;
import java.util.concurrent.*;

/**
 * Created by tangzheng on 16/5/11.
 */
public class DeserializerThreadPoolHolder extends AbstractThreadPoolHolder {

  private static final Logger LOGGER = LoggerFactory
    .getLogger(DeserializerThreadPoolHolder.class);

  private static class InnerHolder {

    private static final DeserializerThreadPoolHolder INSTANCE = new DeserializerThreadPoolHolder();
  }

  private DeserializerThreadPoolHolder() {
    CompositeConfiguration c = new CompositeConfiguration();
    int threadCount = 20;
    String poolName = "Deserializer";
    try {
      c.addConfiguration(new PropertiesConfiguration("deserializerPool.properties"));
      threadCount = c.getInt("deserializer.worker.count", 200);
      poolName = c.getString("deserializer.threadPoolName", "deserializerPool");
    } catch (ConfigurationException e) {
      LOGGER.error("Cannot load config.", e);
    }
    LOGGER.info("Basic deserializer thread pool({} threads) has been built.", threadCount);
    this.name = poolName;
    this.service = Executors
      .newFixedThreadPool(threadCount, new GenericThreadThreadFactory(poolName));
    /*final BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(100);
    this.service = new ThreadPoolExecutor(threadCount, threadCount,
                                             0L, TimeUnit.MILLISECONDS,
                                             queue, new GenericThreadThreadFactory(poolName));*/
  }

  public static final DeserializerThreadPoolHolder getInstance() {
    return InnerHolder.INSTANCE;
  }

}
